require("../util.js");
require("../component/components.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
if(!userInfo){
    location.href="/loginIndex"
}
var key;
var vm = new Vue({
    el:"#myGoods",
    data:{
        uid:"",
        productId:"",
        pageNumber:"1",
        loadFlag:false,
        productsList:{},
        productsList1:[],
        productsList2:[],
        productsList3:[],
        myGoodsList:{},
        number:"",
        circleId:""
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.uid=this.searchObject&&this.searchObject.uid;
        this.productId=this.searchObject&&this.searchObject.productId;
        this.number=this.searchObject&&this.searchObject.number;
        this.type=this.searchObject&&this.searchObject.type;
        this.circleId=this.searchObject&&this.searchObject.circleId;
        this.isMain=this.searchObject&&this.searchObject.isMain;

        if(this.type==1||this.type==2){  //写游记商品展示
            key="myGoodsList-"+userInfo.id;
            this.myGoodsList=JSON.parse(sessionStorage.getItem(key));
            this.getProductList();
        }else if(this.type==3){
            this.getUserProductsList();
        }
        // else if(this.type==2){  //圈子设置
        //     key="circleId-"+userInfo.id;
        //     this.myGoodsList=JSON.parse(sessionStorage.getItem(key));
        //     this.getProductList();
        // }

        //console.log(this.myGoodsList)
        //sessionStorage.removeItem("circleId-7")
    },
    methods:{
        //后退
        getBack:function(){
            if(this.type==1||this.type==3){
                location.href="/cityAd/travelNotes?number="+this.number;
            }else if(this.type==2){
                history.go(-1);
                //location.href="/friend/circleSetup?number="+this.number+"&uid="+this.uid+"&circleId="+this.circleId+"&isMain="+this.isMain;
            }else if(this.type==4){

            }

            // history.go(-1);
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //我的商品列表
        getUserProductsList:function(){
            var that=this;
            var data={
                loginUid:this.uid,
                pageNumber:that.pageNumber
            };
            this.ajax({url:"/user/getUserProductsList",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.productsList=b.json.data;
                    that.productsList1=that.productsList.list;
                    if(that.productsList.pageNumber>1){
                        if(that.productsList.list.length>0) {
                            that.productsList2 = that.productsList2.concat(b.json.data.list);
                        }
                    }else{
                        that.productsList2= that.productsList1;
                    }
                    vm.loadFlag=false;
                    // lazyload();
                }else{
                    vm.$refs.prompt.toast("获取我的商品列表失败！")
                }
                //console.log(that.productsList2)
            });
        },
        //我的商品列表
        getProductList:function(){
            var that=this;
            var data={
                loginUid:this.uid
            };
            this.ajax({url:"/friend/getProductList",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.productsList2= b.json.data;
                }else{
                    vm.$refs.prompt.toast("获取我的商品列表失败！")
                }
            });
        },
        //展示
        getShow:function(item){
            var that=this;
            console.log(item);
            if(that.type==1){
                sessionStorage.setItem(key,JSON.stringify(that.getAdd(item)));
                location.href="/cityAd/travelNotes?number="+that.number;
            }else if(that.type==2){
                if(that.number==5){
                    vm.$refs.prompt.toast("最多可以展示5个商品！");
                }else{
                    var data={
                        loginUid :this.uid,
                        productId:item.productId,
                        circleId:that.circleId
                    };
                    this.ajax({url:"/friend/addCircleProduct",data:data,dataType:"json",method:"post"},function(b){
                        if(b.json.code==0){
                            sessionStorage.setItem(key,JSON.stringify(that.getAdd(item)));
                            location.href="/friend/circleSetup?number="+that.number+"&uid="+that.uid+"&circleId="+that.circleId+"&isMain="+that.isMain;
                        }else{
                            vm.$refs.prompt.toast("增加商品失败！")
                        }
                    });
                }
            }
        },
        //add
        getAdd:function(item){
            var that=this;
            that.myGoodsList=JSON.parse(sessionStorage.getItem(key));
            console.log(this.myGoodsList);
            var flot=true;
            var myGoodsListAll=[];
            var myGoodsListAll1=[];
            var myGoodsList=that.myGoodsList;
            if(myGoodsList&&myGoodsList.length>0){
                for (var j=0;j<myGoodsList.length;j++){
                    // flot=true;
                    if(myGoodsList[j].productId!=item.productId){
                        console.log("111")
                        flot=false;
                        myGoodsList.push(item);
                        // myGoodsListAll=myGoodsList;
                        break;

                    }
                }
                if(!flot){
                    console.log(myGoodsList);
                    myGoodsListAll=myGoodsList;
                }
                if(that.productId){
                    for (var k=0;k<myGoodsListAll.length;k++){
                        // flot=true;
                        if(myGoodsListAll[k].productId==that.productId){
                            console.log("111")
                        }else{
                            myGoodsListAll1.push(myGoodsListAll[k]);
                        }
                    }
                }else{
                    myGoodsListAll1=myGoodsListAll;
                }

            }else{
                console.log("222")
                myGoodsListAll1.push(item);
            }
            return myGoodsListAll1;
        },
        //取消
        getHide:function(item){
            var that=this;
            if(that.type==1){
                console.log(that.number)
                sessionStorage.setItem(key,JSON.stringify(that.getReduce(item)));
                location.href="/cityAd/travelNotes?number="+that.number;
            }else if(that.type==2){
                console.log(that.number)
                var data={
                    loginUid :this.uid,
                    productId:item.productId,
                    circleId:that.circleId
                };
                this.ajax({url:"/friend/deleteCircleProduct",data:data,dataType:"json",method:"post"},function(b){
                    if(b.json.code==0){
                        sessionStorage.setItem(key,JSON.stringify(that.getReduce(item)));
                        location.href="/friend/circleSetup?number="+that.number+"&uid="+that.uid+"&circleId="+that.circleId+"&isMain="+that.isMain;
                    }else{
                        vm.$refs.prompt.toast("删除展位商品失败！");
                    }
                });
                //location.href="/friend/circleSetup?number="+that.number+"&uid="+that.uid+"&circleId="+this.circleId+"&isMain="+this.isMain; //?circleId=43671d1959554fe292856576c89463bd&uid=7&isMain=1
            }
        },
        getReduce:function(item){
            var that=this;
            that.myGoodsList=JSON.parse(sessionStorage.getItem(key));
            var flot=false;
            var myGoodsListAll=[];
            var myGoodsList=that.myGoodsList;
            if(myGoodsList&&myGoodsList.length>0){
                for (var j=0;j<myGoodsList.length;j++){
                    if(myGoodsList[j].productId==item.productId){

                    }else{
                        console.log("5555")
                        myGoodsListAll.push(myGoodsList[j]);
                    }
                }
            }
            return myGoodsListAll;
        },
        //跳转
        getNext:function(){
            if(this.type==1){
                location.href="/addGoods?type=3&number="+this.number;
            }else if(this.type==2){
                location.href="/addGoods?type=2&number="+this.number+"&circleId="+this.circleId;
            }
        }
    },
    watch:{
        productsList2:function(){
            var that=this;
            // console.log(that.productsList2)
            var flot;
            //接口数据
            var productsList3=that.productsList2;
            var myGoodsList=that.myGoodsList;
            for (var i=0;i<productsList3.length;i++){
                if(myGoodsList&&myGoodsList.length>0){
                    for (var j=0;j<myGoodsList.length;j++){
                        flot=true;
                        if(productsList3[i].productId==myGoodsList[j].productId){
                            flot=false;
                            break;
                        }
                    }
                }else{
                    flot=true;
                }
                if(!flot){
                    productsList3[i].isShow=1;
                }else{
                    productsList3[i].isShow=0;
                }
            }
            that.productsList3=productsList3;
            console.log(productsList3)
        }
    }
});
//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;    return scrollTop;
}
//可视窗口高度
function getWindowHeight() {
    var windowHeight = 0;
    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}
//滚动条滚动高度
function getScrollHeight() {
    var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
    if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
    return scrollHeight;

}
window.onscroll=function(){
    if(vm.type==1){
        var loadFlag = vm.productsList.lastPage;
        //监听事件内容
        if(getScrollHeight()==getWindowHeight() + getDocumentTop()){
            //当滚动条到底时,这里是触发内容
            if(!loadFlag){
                vm.loadFlag=true;
                vm.pageNumber++;
                vm.getUserProductsList();
                // lazyload();
            }else{
                vm.$refs.prompt.toast("没有更多了！")
            }
        }
    }
};
// function lazyload(){
//     $("img.lazy").lazyload();
// }